import React, {PropsWithChildren, useState} from 'react'
import {Dialog, DialogProps, Input, InputNumber} from "@nutui/nutui-react-taro";

export interface PromptProps extends PropsWithChildren {
    onConfirm: (value: string) => Promise<void>
    visible: DialogProps["visible"]
    onCancel: DialogProps["onCancel"]
    title: DialogProps["title"]
    placeholder?:string
    initialValue?:string
}

const Prompt: React.FC<PromptProps> = (props) => {
    const [value,setValue] = useState<string|undefined>(props.initialValue)

    function onAsyncConfirm() {
        return async () => {
            await props.onConfirm(value!)
            props.onCancel ? props.onCancel() : undefined
        };
    }

    return <>
        <Dialog
            {...props}
            content={
                // Dialog 中无法使用Form
                <>
                    <Input onConfirm={onAsyncConfirm} autoFocus value={value} onChange={setValue}
                          placeholder={props.placeholder}/>
                </>
            }
            onConfirm={onAsyncConfirm()}
            disableConfirmButton={!value}
        />
    </>
}

export default Prompt


